﻿
//
//
//

namespace Microsoft.OpenTurf.Silverlight.Common
{
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Windows;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using Microsoft.OpenTurf.Silverlight.Shared;

    public abstract class MenuControlBase : OpenTurfViewModelBase
    {
        #region Properties
        public ObservableCollection<IFeedSource> MenuItems { get; set; }
        #endregion

        #region Constructor
        public MenuControlBase()
        {
        }
        #endregion

        #region Commands
        public RelayCommand<RoutedEventArgs> MenuClicked { get; private set; }
        #endregion

        #region Methods
        protected void Initialize()
        {
            RegisterEvents();
            RegisterMessageHandlers();
        }
        private void RegisterEvents()
        {
            MenuClicked = new RelayCommand<RoutedEventArgs>(e => HandleClick(e));
        }
        private void RegisterMessageHandlers()
        {
            MenuItems = new ObservableCollection<IFeedSource>();
            Messenger.Default.Register<IFeedSource>(this, NotificationTokens.AddFeedSource, (source) => AddFeedSource(source));
        }

        private void AddFeedSource(IFeedSource source)
        {
            MenuItems.Add(source);
            RaisePropertyChanged("MenuItems");
        }

        public abstract void HandleClick(RoutedEventArgs e);
        #endregion
    }
}